package com.lwq.project.controller.user;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.lwq.project.model.User;
import com.lwq.project.model.User2role;
import com.lwq.project.service.RoleService;
import com.lwq.project.service.User2roleService;
import com.lwq.project.service.UserService;
import com.lwq.project.utils.UtilsTools;
import com.lwq.project.utils.page.Pages;

/**
* @author 作者 linweiqiang
* @version 创建时间：2017年9月25日 上午11:02:20
* 类说明
*/
@Controller
@RequestMapping("/user")
public class UserController {
	
	
	@Autowired
	private UserService userService;
	@Autowired
	private RoleService roleService;
	@Autowired
	private User2roleService user2roleService;
	
	@RequestMapping("/userView.action")
	public String initView(){
		return "main/user/userView";
	}
	
	@RequestMapping("/userTable.action")
	public String initList(Model model,Pages pages){
		Map<String,Object> map = new HashMap<String, Object>();
		map.put("pages", pages);
		int count = userService.getCount(map);
		pages.setNumberCount(count);
		List<User> list = userService.getUserByAll(map);
		model.addAttribute("pages", pages);
		model.addAttribute("userList", list);
		return "main/user/userTable";
	}
	
	@RequestMapping("/userEdit.action")
	public String initEdit(Model model,String uId){
		if(!StringUtils.isBlank(uId)){
			Map<String,Object> map = new HashMap<String,Object> ();
			map.put("uId", uId);
			List<User> list= userService.getUserByAll(map);
			User user = list.get(0);
			model.addAttribute("uRole", roleService.getRoleByUid(uId));
			model.addAttribute("user", user);
		}
		model.addAttribute("rList", roleService.getAllRole());
		return "main/user/userEdit";
	}
	
	@RequestMapping("/userSave.action")
	@ResponseBody
	public String initSave(User user,String rId){
		if(StringUtils.isBlank(user.getuId())){
			user.setuId(UtilsTools.getPrimary());
			user.setuPassword("123456");
			userService.insertUser(user);
		}else{
			user.setuPassword(null);
			userService.updateUser(user);
		}
		
		if(StringUtils.isBlank(user2roleService.getRoleIdByUserId(user.getuId()))){
			User2role ur = new User2role();
			ur.setUrId(UtilsTools.getPrimary());
			ur.setuId(user.getuId());
			ur.setrId(rId);
			user2roleService.insertUserRole(ur);
		}else{
			Map<String ,Object> map = new HashMap<String, Object>();
			map.put("rId", rId);
			map.put("uId", user.getuId());
			user2roleService.updateByUid(map);
		}
		return "success";
	}
	
	@RequestMapping("/userDel.action")
	@ResponseBody
	public String initDel(String uId){
		userService.delUser(uId);
		return "success";
	}
}
